package com.zgkj.Zip.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import com.bsfit.connectionpool.ConnectionSource;
import com.zgkj.Zip.unzip;
/**
 * <p>Title: 数据库操作工具类</p>
 * <p>Description: 提供对数据库的各种常用处理方法</p>
 * @version 1.0
 */
public class DBUtil implements DBUtilI{
	private Connection conn=null;
	private ResultSet rs=null;
	private Statement qryStmt=null;
	private Statement uptStmt=null;
	private PreparedStatement queryPs = null;
	  /**
	   * 功能：在初始化类的时候，即创建好数据库链接，获得数据库连接,Statement
	   * @param
	   * @return    void
	   * @exception 获取数据源错
	   */
	public  DBUtil() throws ClassNotFoundException,SQLException{
		createConn();
	}

	private synchronized void  createConn()throws ClassNotFoundException,SQLException{
		conn=ConnectionSource.getConnection();
	}

	private void setQryStmt() throws SQLException{
		if(qryStmt==null){
			conn.setAutoCommit(false);
			qryStmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		}
	}

	private void setUptStmt() throws SQLException{
		if(uptStmt==null){
			conn.setAutoCommit(false);
			uptStmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		}
	}

	  /**
	   * 功能：在初始化类的时候，即创建好数据库链接，获得数据库连接,Statement
	   * @param
	   * @return    void
	   * @exception 获取数据源错
	   */
	public DBUtil(String ls_dbdriver,String ls_connstr,String ls_username,String ls_password) throws ClassNotFoundException,SQLException{
       Class.forName(ls_dbdriver);
       conn=DriverManager.getConnection(ls_connstr,ls_username,ls_password);
       conn.setAutoCommit(false);
	}

	  /**
	   * 功能：获得已经创建好的数据库链接
	   * @param
	   * @return    Connection
	   * @exception 获取数据源错
	   */
	@Override
	public Connection getConnection(){
		return conn;
	}

	  /**
	   * 功能：获得已经创建好的数据库链接
	   * @param
	   * @return    Statement
	   */
	@Override
	public Statement getQryStatement(){
		return qryStmt;
	}

	@Override
	public Statement getUptStatement() throws SQLException{
		return uptStmt;
	}

    @Override
	public ResultSet query(String sql) throws SQLException{
    	closeResultSet();
    	setQryStmt();
        rs=qryStmt.executeQuery(sql);
    	return rs;
    }

    public ResultSet queryPS() throws SQLException{
    	closeResultSet();
        rs=queryPs.executeQuery();
    	return rs;
    }
    @Override
	public int update(String sql)throws SQLException{
    	return update(sql,0);
    }


    public void setPsSql(String sql)throws SQLException{
    	conn.setAutoCommit(false);
    	this.queryPs=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    }
    public void setPsSqlStrValue(int parameterIndex,String x)throws SQLException{
    	this.queryPs.setString(parameterIndex, x);
    }

    public void setPsSqlIntValue(int parameterIndex,int x)throws SQLException{
    	this.queryPs.setInt(parameterIndex, x);
    }

    public void setPsSqlLongValue(int parameterIndex,Long x)throws SQLException{
    	this.queryPs.setLong(parameterIndex, x);
    }
    public void closePS() {
		try {
			if(this.queryPs!=null){
				queryPs.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    public void closeStmt() {
		try {
			if(this.queryPs!=null){
				queryPs.close();
			}
			if(this.qryStmt!=null){
				qryStmt.close();
			}
			if(this.uptStmt!=null){
				uptStmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

    @Override
	public int update(String sql,int code) throws SQLException{
    	int count=0;
    	setUptStmt();
		try{
			count = uptStmt.executeUpdate(sql);
		}catch(SQLException e){
			rollback();
			if(code==1){
				count=0;
			}else{
				throw new SQLException(e.toString());
			}
		}
		if(count!=0){
			commit();
		}else{
			rollback();
		}
    	return count;
    }

    @Override
	public void addBatch(String sql) throws SQLException{
    	setUptStmt();
    	uptStmt.addBatch(sql);
    }


    @Override
	public void addBatch(ArrayList<String> list) throws SQLException{
    	setUptStmt();
    	for (String element : list) {
    		unzip.log(element);
    		uptStmt.addBatch(element);
    	}
    }

    @Override
	public int executeBatch() throws SQLException{
		return  executeBatch(0);
    }
    /*
     * code 0:判断所有的sql是否成功
     *      1:执行过程中不出错就可以
     */
    @Override
	public int executeBatch(int code) throws SQLException{
    	int[] re=null;
    	int ri=0;
    	setUptStmt();
    	try{
    		re=uptStmt.executeBatch();
		}catch(SQLException e){
			rollback();
			e.printStackTrace();
			throw new SQLException(e.toString());
		}
		if(re!=null){
			boolean bool=false;
			if(code==0){
				for (int element : re) {
					if(element==0){
						rollback();
						bool=true;
						break;
					}
				}
				if(!bool){
					commit();
					ri=1;
				}
			}else if(code==1){
				commit();
				ri=1;
			}else{
				commit();
				ri=1;
			}
		}
		return  ri;
    }

    @Override
	public void commit() throws SQLException{
    	conn.commit();
    }

    @Override
	public void rollback() throws SQLException{
    	conn.rollback();
    }

    @Override
	public synchronized void close() {
    	try{
    		if(rs!=null){
        	    closeResultSet();
        	}
        	if(uptStmt!=null) {
        		uptStmt.close();
        	}
        	if(qryStmt!=null) {
        		qryStmt.close();
        	}
        	if(queryPs!=null) {
        		queryPs.close();
        	}
        	if(conn!=null){
        		conn.close();
        	}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }


    private void closeResultSet() throws SQLException{
    	if(rs!=null){
    	    rs.close();
    	}
    }




 /**
  * 功能：获取数据库一个参数的值
  * @param     String sql
  * @return    ArrayList
  */
  @Override
public  ArrayList getOneParValueByArrayList(String sql)throws SQLException{
	  query(sql);
	  ArrayList alist=new ArrayList();
	  while(rs.next()){
		alist.add(rs.getString(1));
	  }
	  closeResultSet();
	  return alist;
  }

  /**
   * 功能：获取数据库一个参数的值
   * @param     String sql
   * @return    String[]
   */

  @Override
public String[] getOneValueByStringArray(String sql) throws SQLException{
	  query(sql);
	  rs.last();
	  int count=rs.getRow();
	  if(count==0)return null;
	  rs.beforeFirst();
	  String[] strArray=new String[count];
	  int num=0;
	  while(rs.next()){
		  strArray[num]=rs.getString(1);
		  num++;
	  }
	  closeResultSet();
	  return strArray;
  }

  /**
   * 功能：获取数据库一个参数的值
   * @param     String sql
   * @return    String[]
   */

  @Override
public String[] getOneNoteValuesByStringArray(String sql) throws SQLException{
	  query(sql);
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String[] strArray=new String[columnCount];
	  if(rs.next()){
	      for(int i=0;i<columnCount;i++){
	    	  strArray[i]=rs.getString(1+i);
	      }
	  }else{
		  return null;
	  }
	  closeResultSet();
	  return strArray;
  }

  /**
   * 功能：获取最大值
   * @param     String sql
   * @return    String[]
   */
  @Override
public int getMaxValue(String sql) throws SQLException,Exception{
	  query(sql);
      int maxValue=0;
	  if(rs.next()){
		  if(rs.getString(1)==null||rs.getString(1).equals("")){
			  maxValue=1;
		  }else{
			  maxValue=Integer.parseInt(rs.getString(1))+1;
		  }
	  }else{
		  maxValue=1;
	  }
	  closeResultSet();
	  return maxValue;
  }

  /**
   * 功能：获取最大值
   * @param     String sql
   * @return    String[]
   */
  @Override
public long getMaxValueByLong(String sql) throws SQLException,Exception{
	  query(sql);
      long maxValue=0;
	  if(rs.next()){
		  if(rs.getString(1)==null||rs.getString(1).equals("")){
			  maxValue=1;
		  }else{
			  maxValue=Long.parseLong(rs.getString(1))+1;
		  }
	  }else{
		  maxValue=1;
	  }
	  closeResultSet();
	  return maxValue;
  }

  /**
   * 功能：获取最大值
   * @param     String sql
   * @return    String[]
   */
  @Override
public Integer getMaxValue2(String sql) throws SQLException,Exception{
	  query(sql);
      int maxValue=0;
	  if(rs.next()){
		  if(rs.getString(1)==null||rs.getString(1).equals("")){
			  maxValue=1;
		  }else{
			  maxValue=Integer.parseInt(rs.getString(1))+1;
		  }
		  closeResultSet();
	  }else{
		  closeResultSet();
		  return null;
	  }

	  return maxValue;
  }


  @Override
public int getOneNoteOneParValue(String sql,int num)throws SQLException,Exception{
	  query(sql);
      int Value=0;
	  if(rs.next()){
		  if(rs.getString(1)==null||rs.getString(1).equals("")){
			  Value=num;
		  }else{
			  Value=Integer.parseInt(rs.getString(1));
		  }
	  }else{
		  Value=num;
	  }
	  closeResultSet();
	  return Value;
  }

  @Override
public long getOneNoteOneParValueByLong(String sql,int num)throws SQLException,Exception{
	  query(sql);
      long Value=0;
	  if(rs.next()){
		  if(rs.getString(1)==null||rs.getString(1).equals("")){
			  Value=num;
		  }else{
			  Value=rs.getLong(1);
		  }
	  }else{
		  Value=num;
	  }
	  closeResultSet();
	  return Value;
  }


  @Override
public String getOneNoteOneParValue(String sql)throws SQLException,Exception{
	  query(sql);
      String value=null;
	  if(rs.next()){
		  value=rs.getString(1);
	  }
	  closeResultSet();
	  return value;
  }




  /**
   * 功能：返回查询结果集
   * @param     String sql
   * @return    String[][]
   * @exception
   */
  @Override
public String[][] getParValuesByArray(String sql) throws SQLException{
	  query(sql);
	  rs.last();
	  int count=rs.getRow();
	  if(count==0)return null;
	  rs.beforeFirst();
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String[][] strArray=new String[count][columnCount];
	  int num=0;
	  String value="";
	  while(rs.next()){
		  for(int i=0;i<columnCount;i++){
			  value=(rs.getString(i+1)==null)?"":rs.getString(i+1);
			  strArray[num][i]=value.trim();
		  }
		  num++;
	  }
	  closeResultSet();
	  return strArray;
  }

  /**
   * 功能：返回查询结果集
   * @param     String sql
   * @return    String[][]
   * @exception
   */
  @Override
public String[][] getParValuesByArray(String sql,int length) throws SQLException{
	  query(sql);
	  rs.last();
	  int count=rs.getRow();
	  if(count==0)return null;
	  rs.beforeFirst();
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  if(length<count)count=length;
	  String[][] strArray=new String[count][columnCount];
	  int num=0;
	  String value="";
	  while(rs.next()&&length-->0){
		  for(int i=0;i<columnCount;i++){
			  value=(rs.getString(i+1)==null)?"":rs.getString(i+1);
			  strArray[num][i]=value.trim();
		  }
		  num++;
	  }
	  closeResultSet();
	  return strArray;
  }


  @Override
public ArrayList getParValuesByArrayList(String sql) throws SQLException{
	  query(sql);
      ArrayList list=new ArrayList();
      ArrayList oneList=null;
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String value="";
	  while(rs.next()){
		  oneList=new ArrayList();
		  for(int i=0;i<columnCount;i++){
			  value=(rs.getString(i+1)==null)?"":rs.getString(i+1);
			  oneList.add(rs.getString(i+1));
		  }
		  list.add(oneList);
	  }
	  closeResultSet();
	  return list;
  }

  @Override
public ArrayList getStatParValuesByArrayList(String sql) throws SQLException{
	  query(sql);
      ArrayList list=new ArrayList();
      ArrayList oneList=null;
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String value="";
	  while(rs.next()){
		  oneList=new ArrayList();
		  if(rs.getString(1).equals(value))
			  oneList.add("");
		  else{
			  oneList.add(value=rs.getString(1));
		  }

		  for(int i=1;i<columnCount;i++){
			  oneList.add(rs.getString(i+1));
		  }
		  list.add(oneList);
	  }
	  closeResultSet();
	  return list;
  }

  @Override
public int getStatParValuesByArrayList(String sql,ArrayList list) throws SQLException{
	  query(sql);
      ArrayList oneList=null;
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String value="";
	  int num=0;
	  while(rs.next()){
		  num++;
		  oneList=new ArrayList();
		  if(rs.getString(1).equals(value))
			  oneList.add("");
		  else{
			  oneList.add(value=rs.getString(1));
		  }

		  for(int i=1;i<columnCount;i++){
			  oneList.add(rs.getString(i+1));
		  }
		  list.add(oneList);
	  }
	  closeResultSet();
	  return num;
  }

  /**
   * 功能：返回查询结果集
   * @param     String sql
   * @return    ArrayList
   * @exception
   */
  @Override
public ArrayList getOneValuesByArrayList(String sql) throws SQLException{
	  query(sql);
	  ArrayList alist=new ArrayList();
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  while(rs.next()){
		  for(int i=0;i<columnCount;i++){
			  alist.add(rs.getString(i+1));
		  }
	  }
	  closeResultSet();
	  return alist;
  }


  /**
   * 功能：返回查询结果集
   * @param     String sql
   * @return    ArrayList
   * @exception
   */
  @Override
public ArrayList getOneValuesByArrayList(String sql,String firstValue,int code) throws SQLException{
	  query(sql);
	  ArrayList alist=new ArrayList();
	  alist.add(firstValue);
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  if(rs.next()){
		  for(int i=0;i<columnCount;i++){
			  if(rs.getString(i+1)!=null){
				  alist.add(rs.getString(i+1));
			  }else{
				  if(code==1){
					  alist.add("0");
				  }else{
					  alist.add(rs.getString(i+1));
				  }
			  }

		  }
	  }else{
		  alist=new ArrayList();
	  }
	  closeResultSet();
	  return alist;
  }


  @Override
public ArrayList getOneValuesByArrayList(String sql,String firstValue) throws SQLException{
	  return getOneValuesByArrayList(sql,firstValue,0);
  }


  @Override
public ArrayList getOneValuesByArrayList(String sql,ArrayList firstValue) throws SQLException{
	  query(sql);
	  ArrayList alist=new ArrayList();
	  for(int i=0;i<firstValue.size();i++){
		  alist.add(firstValue.get(i).toString());
	  }
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  if(rs.next()){
		  for(int i=0;i<columnCount;i++){
			  if(rs.getString(i+1)!=null){
				  alist.add(rs.getString(i+1));
			  }else{
				  alist.add("0");
			  }
		  }
	  }
	  closeResultSet();
	  return alist;
  }





  /**
   * 功能：将单条记录封装成HashMap形式返回，Key为字段名,value为字段值
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  public  HashMap getRecord(String sql) throws SQLException{
	  query(sql);
	  ResultSetMetaData rsmeta = rs.getMetaData();
	  HashMap   recordHashMap = null;    String colName;    int i;
	  int numOfCols = rsmeta.getColumnCount();
	  if(rs.next()) {
	     recordHashMap=new HashMap();
	     for (i = 1;i <= numOfCols;i++)	{
	        colName = rsmeta.getColumnName(i);
	        if( rs.getString(colName) == null)  continue;
	        String colVal= rs.getString(colName);
	        recordHashMap.put(colName.toLowerCase(),colVal);
	     }
	  }else{
		  return null;
	  }
	  closeResultSet();
	  return recordHashMap;
  }

  /**
   * 功能：将单条记录封装成HashMap形式返回，Key为字段名,value为字段值
   * @param     ResultSet，结果集 HashMap hm
   * @return    HashMap  0:结果为空 1：查询到结果
   * @exception
   */
  @Override
public  int getRecordHm(String sql,HashMap hm) throws SQLException{
	  query(sql);

	  ResultSetMetaData rsmeta = rs.getMetaData();
	  String colName;
	  int numOfCols = rsmeta.getColumnCount();
	  if(rs.next()) {
	     for (int i = 1;i <= numOfCols;i++)	{
	        colName = rsmeta.getColumnName(i);
	        if( rs.getString(colName) == null)  continue;
	        String colVal= rs.getString(colName);
	        hm.put(colName.toLowerCase(),colVal);
	     }
	  }else{
		  closeResultSet();
		  return 0;
	  }
	  closeResultSet();
	  return 1;
  }



  /**
   * 功能：将[n]记录封装成HashMap[]形式返回，Key为字段名,value为字段值
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  @Override
public  HashMap[] getRecordHashMapArray(String sql) throws SQLException{
	  int count=getCount(sql);
	  rs.beforeFirst();
	  HashMap[] recordHashMaps=new HashMap[count];
	  ResultSetMetaData rsmeta = rs.getMetaData();
	  HashMap recordHashMap=null;
	  String colName;
	  int num=0;
	  int numOfCols = rsmeta.getColumnCount();
	  while(rs.next()) {
	     recordHashMap=new HashMap();
	     for(int i=1;i<=numOfCols;i++)	{
	        colName = rsmeta.getColumnName(i);
	        if(rs.getString(colName) == null)  continue;
	        String colVal= rs.getString(colName);
	        recordHashMap.put(colName.toLowerCase(),colVal);
	     }
	     recordHashMaps[num++]=recordHashMap;
	  }
	  closeResultSet();
	  return recordHashMaps;
  }

  /**
   * 功能：将[n]记录封装成HashMap[]形式返回，Key为字段名,value为字段值 PreparedStatement
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  public  HashMap[] getRecordHashMapArrayPS() throws SQLException{
	  int count=getCountPS();
	  rs.beforeFirst();
	  HashMap[] recordHashMaps=new HashMap[count];
	  ResultSetMetaData rsmeta = rs.getMetaData();
	  HashMap recordHashMap=null;
	  String colName;
	  int num=0;
	  int numOfCols = rsmeta.getColumnCount();
	  while(rs.next()) {
	     recordHashMap=new HashMap();
	     for(int i=1;i<=numOfCols;i++)	{
	        colName = rsmeta.getColumnName(i);
	        if(rs.getString(colName) == null)  continue;
	        String colVal= rs.getString(colName);
	        recordHashMap.put(colName.toLowerCase(),colVal);
	     }
	     recordHashMaps[num++]=recordHashMap;
	  }
	  closeResultSet();
	  return recordHashMaps;
  }


  /**
   * 功能：将两参数的记录结果封装成HashMap形式返回，Key为id,value为name
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  @Override
public HashMap getIdToName(String sql) throws SQLException{
	  query(sql);
	  HashMap recordHashMap=null;
	  recordHashMap=new HashMap();
	  while(rs.next()) {
	      recordHashMap.put(rs.getString(1),rs.getString(2));
	  }
	  closeResultSet();
	  return recordHashMap;
  }


  @Override
public int getCount(String sql)throws SQLException{
	  query(sql);
	  rs.last();
	  return rs.getRow();
  }
  public int getCountPS()throws SQLException{
	  queryPS();
	  rs.last();
	  return rs.getRow();
  }
  @Override
public boolean isResultNull(String sql) throws SQLException{
	  try{
		  query(sql);
		  if(rs.next()){
			  return true;
		  }
		  return false;
	  }finally{
		  closeResultSet();
	  }

  }



  /**
   * 功能：将双参数封录封装成HashMap(String key，String value)形式返回，双字段
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  @Override
public HashMap getTwoFieldKeyByValue(String sql) throws SQLException{
	  query(sql);
	  HashMap recordHashMap=null;
	  ArrayList alist=new ArrayList();
	  String key=null;
	  while(rs.next()){
		  key=rs.getString(1);
		  alist.add(rs.getString(2));
	  }
	  if(!alist.isEmpty()){
		  recordHashMap=new HashMap();
		  recordHashMap.put(key, alist);
	  }
	  closeResultSet();
	  return recordHashMap;
}


  /**
   * 功能：将双参数封装成HashMap(String key，ArrayList value)形式返回，双字段
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  @Override
public HashMap getTwoFieldKeyByValues(String sql) throws SQLException  {
      query(sql);
	  HashMap   recordHashMap = null;
	  recordHashMap=new HashMap();
	  ArrayList alist=null;
	  String key="";
	  while(rs.next()) {
		  key=rs.getString(1);
		  if(recordHashMap.get(key)==null){
			  alist=new ArrayList();
			  alist.add(rs.getString(2));
			  recordHashMap.put(key, alist);
		  }else{
			  alist=(ArrayList)recordHashMap.get(key);
			  alist.add(rs.getString(2));
		  }
	  }
	  closeResultSet();
	  return recordHashMap;
}





  /**
   * 功能：将单条记录封装成HashMap形式返回,参数1为key,参数2,3,4...在ArrayList里面为value
   * @param     ResultSet，结果集
   * @return    HashMap
   * @exception
   */
  @Override
public HashMap getIdToNote(String sql) throws SQLException{
      query(sql);
	  HashMap recordHashMap=null;
	  recordHashMap=new HashMap();
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  ArrayList alist=null;
	  while(rs.next()) {
		  alist=new ArrayList();
		  for(int i=0;i<columnCount;i++){
			  alist.add(rs.getString(i+1));
		  }
	      recordHashMap.put(rs.getString(1),alist);
	  }
	  closeResultSet();
	  return recordHashMap;
}

  /**
   * 功能：将单条记录封装成String[n][2]形式返回 String[n][0]:字段名称 String[n][1]:值
   * @param     ResultSet，结果集
   * @return     结果为空时返回null;
   * @throws SQLException
   * @exception
   * @author
   */

  @Override
public String[][] getOneNoteByFieldToValue(String sql) throws SQLException{
	  query(sql);
	  ResultSetMetaData rsmd=rs.getMetaData();
	  int columnCount=rsmd.getColumnCount();
	  String[][] strarr=new String[columnCount][2];
	  if(rs.next()){
		  for(int i=0;i<columnCount;i++){
			  strarr[i][0]=rsmd.getColumnName(i+1);
			  strarr[i][1]=rs.getString(rsmd.getColumnName(i+1));
		  }

	  }else{
		  return null;
	  }
	  closeResultSet();
	  return strarr;
  }

  @Override
public int getNumber(String sql) throws SQLException{
	  try{
		  query(sql);
		  if(rs.next()){
			  if(rs.getString(1)==null||rs.getString(1).equals(""))
				  return 0;
			  else
				  return Integer.parseInt(rs.getString(1));
		  }else{
			  return 0;
		  }
	  }finally{
		  closeResultSet();
	  }

  }

  @Override
public long getLongNumber(String sql) throws SQLException{
	  try{
		  query(sql);
		  if(rs.next()){
			  if(rs.getString(1)==null||rs.getString(1).equals(""))
				  return 0;
			  else
				  return rs.getLong(1);
		  }else{
			  return 0;
		  }
	  }finally{
		  closeResultSet();
	  }

  }

  /**
   * 调试用main方法
   */
  public static void main(String[] args) throws Exception {
	  DBUtil db=new DBUtil("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@192.168.22.235:1521:auditdb","topzf","topzf");
	  for (int i = 0; i < 1; i++) {
	    	ResultSet rs=null;
	    	rs=db.query("select "+i+" from dual");
	    	//rs.close();
	    	Connection connection = db.getConnection();

	    	PreparedStatement ps1 = connection.prepareStatement("select "+1+" from dual");
	    	for (int j = 0; j < 500; j++) {
	    		System.out.println(ps1.execute());
			}

	    	System.out.println(rs.getRow());
	    	System.out.println(i);
	}

  }



}
